//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const api=require("api");
    const sdk=require("sdk");
    const weui=require('weui');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
                user_name:'',
                user_code:'',
            }
        },
        computed:{
            isBack:function () {
                return this.$store.getters.getIsBack;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {

            })
        },
        created:function () {
        },
        methods:{
            confirm:function (title,callback) {
                var v=this;
                weui.confirm(title, {
                    title: '',
                    buttons: [{
                        label: '取消',
                        type: 'default',
                        onClick: function(){ v.$router.push({ path: '/index' }); }
                    }, {
                        label: '确定',
                        type: 'primary',
                        onClick: function(){ callback();}
                    }]
                });
            },
            submit:function () {
                var v=this;
                if(v.user_name.length==0){
                    weui.topTips("请输入姓名！");
                    return;
                }
                if(v.user_code.length===0){
                    weui.topTips("请输入身份证号码！");
                    return;
                }
                var loading = weui.loading('正在验证……');
                api.postCertification(v.user_name,v.user_code,v.user_info).then(function (data) {
                    if(data.user_token_invalid){
                        v.$store.commit("logOut");
                        v.$router.push({path: '/phone'});
                        return;
                    }
                    if(data.user_certification&&data.user_certification.return_code==="SUCCESS"){
                        //刷新用户信息数据
                        v.$store.dispatch('UserInfo',v.user_info).then(function (d) {
                            weui.toast("实名认证成功！",{duration:1000,callback:function () {
                                //实名认证判断
                                if(d.deposit==0){//已交押金判断
                                    v.confirm('您还未缴纳押金，是否去缴纳？', function () {
                                        v.$router.push({ path: '/deposit' });
                                    });
                                }else{
                                    v.$router.push({ path: '/index' });
                                }
                            }});
                        }).catch(function (e) {
                            weui.topTips(e||"网络异常！");
                        });
                    }else {
                        weui.topTips("身份认证失败，请检查您的姓名或身份证号码是否有误！");
                    }
                }).catch(function (e) {
                    weui.topTips("网络异常，请刷新页面再试!");
                }).finally(function () {
                    loading.hide();
                });
            }
        }
    });
});